// pages/detail/detail.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    actionHidden: true,
    animationData: {},
    iconCollection: 'i_collection',
    companyLogo: "/images/com_logo.jpg",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.loginwin = this.selectComponent("#loginwin");
    var that = this;
    this.loginwin.showDialog(function(res){
      if(res!==false){
        //登陆成功
        if (options.id) {
          //分享式
          that.getPositionDetail(options.id);
        } else {
          //二维码进入
          var scene = decodeURIComponent(options.scene);
          var id = scene.split('&')[0].split('=')[1];
          var keyVal = scene.split('&')[1].split('=')[1];
          that.getPositionDetail(id);
          app.apiPost(app.apiList.getScanLog, {
            position_id: id,
            key: keyVal
          }, function (res) {
            if (res.code == 200) {
              console.log('日志纪录成功')
            }
          })
        }
      }else{
        app.alert('登陆失败，请联系管理员');
      }
      
    });
   

  },

  loginedGetUserInfo: function (res) {
    console.log('loginevent?');
    // 用户点击授权后，这里可以做一些登陆操作
    if (res) {
    }
  },
  getPositionDetail: function(id) {
    var that = this;
    app.apiGet(app.apiList.positionDetail, {
      id: id
    }, function(res) {
      if (res.code == 200) {
        app.hideloading();
        var iconCollection = res.data.collection ? 'i_collection_active' : 'i_collection';
        that.setData({
          id: id,
          detail: res.data,
          iconCollection: iconCollection,
          collection: res.data.collection ? true : false,
          image_piiik: res.data.image_piiik,
          image_circle_friends: res.data.image_circle_friends,
          image_article: res.data.image_article,
        })
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    var that = this;
    var company = that.data.detail.company;
    var position = that.data.detail.positionName;
    var salary = that.data.detail.salary;
    var title = company + ' ' + position + ' ' + '[' + salary + ']';
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: title,
      path: 'pages/detail/detail?id=' + that.data.id,
      success: function(res) {
        // 转发成功
        app.apiPost(app.apiList.getShareCount, {
          id: that.data.id
        }, function(res) {
          if (res.code == 200) {
            console.log('统计成功~');
          }
        })
        console.log(res.errMsg)
      },
      fail: function(res) {
        // 转发失败
        console.log(res.errMsg)
      }
    }
  },

  //分享
  showAction: function() {
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: 'linear'
    })
    animation.bottom('0').step()

    this.setData({
      actionHidden: false,
      animationData: animation.export(),
    })
    this.setData({

    })
  },
  closeAction: function() {
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: 'linear'
    })
    animation.bottom('-568rpx').step()
    this.setData({
      actionHidden: true,
      animationData: animation.export(),
    })
  },

  //分享到朋友圈
  shareAppFriends: function() {
    let img = this.data.image_circle_friends
    wx.previewImage({
      current: img, // 当前显示图片的http链接
      urls: [img] // 需要预览的图片http链接列表
    })
  },
  //生成公众号
  shareAppPublic: function() {
    let img = this.data.image_article
    wx.previewImage({
      current: img, // 当前显示图片的http链接
      urls: [img] // 需要预览的图片http链接列表
    })
  },
  //生成长图
  shareLongImg: function() {
    let img = this.data.image_piiik
    wx.previewImage({
      current: img, // 当前显示图片的http链接
      urls: [img] // 需要预览的图片http链接列表
    })
  },
  //获取联系方式
  contactTap: function() {
    var contact = this.data.detail.contact || app.config.defaultContact
    var msg = 'HR邮箱:' + contact;
    app.apiPost(app.apiList.getEmailCount, {
      id: this.data.id
    }, function(res) {
      if (res.code == 200) {
        wx.showModal({
          content: msg,
          showCancel: false,
          confirmColor: '#41a4f4',
          confirmText: '知道了'
        });
      }
    })

  },
  //收藏
  collectionTap: function() {
    var that = this;
    if (this.data.collection) {
      this.data.collection = false;
      var title = '取消成功';
      var iconCollection = 'i_collection';
      var action = 2;
    } else {
      this.data.collection = true;
      var title = '收藏成功';
      var iconCollection = 'i_collection_active';
      var action = 1;
    }
    app.apiPost(app.apiList.collections, {
      action: action,
      id: this.data.id
    }, function(res) {
      if (res.code == 200) {
        wx.showToast({
          title: title,
          icon: 'success',
          mask: true,
          duration: 1500
        })
        that.setData({
          iconCollection: iconCollection
        })
      }
    })

  },
  //查看更多职位
  seeMoreBtn: function() {
    if (this.data.detail.province == '北京市' || this.data.detail.province == '上海市' || this.data.detail.province == '天津市' || this.data.detail.province == '重庆市') {
      this.data.detail.city = this.data.detail.province;
    }


    wx.navigateTo({
      url: '/pages/search/search?value=' + this.data.detail.positionName + '&city=' + this.data.detail.city,
    })
  }
})